package br.com.revenda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import br.com.revenda.conexao.conectionFactory;
import br.com.revenda.modelo.Usuario;

public class LoginDAO {

	private Connection conn;
	public LoginDAO(){
	this.conn = new conectionFactory().getConnection();  	
	}
	
	public Usuario efetuaLogin(String user , String senha , Integer autorizado){
		Usuario u = new Usuario();
		try {
			
			String sql = "select * from usuario where login=? and senha=? and autorizado=?";
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, user);
			pstm.setString(2, senha);
			pstm.setInt(3, autorizado);
			ResultSet rs = pstm.executeQuery();
			
			
			if(rs.next()){			
			u.setLogin(rs.getString("login"));
			u.setStatus(rs.getInt("status"));
			}else{
				u.setLogin("erro");							
			}
			rs.close();
			pstm.close();
			conn.close();			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return u;
	}
	
}
